Skip to content

Interaction class

Attributes: [DebuggerDisplay("Interaction")]

Defined in

Namespace: ReactiveUI.Reactive Assembly: ReactiveUI.Reactive.dll Full name: ReactiveUI.Reactive.Interaction<T1, T2> Modifiers: public

Summary

View source

Represents an interaction between collaborating application components.

Applies to

net10.0, net10.0-maccatalyst26.0, net10.0-desktop1.0, net10.0-browserwasm1.0, net10.0-tvos26.0, net10.0-windows10.0.19041, net10.0-macos26.0, net10.0-ios26.0, net10.0-android36.0, net9.0, net9.0-tvos18.0, net9.0-macos15.0, net9.0-maccatalyst18.0, net9.0-windows10.0.19041, net9.0-ios18.0, net9.0-desktop1.0, net8.0, net8.0-windows10.0.19041, net8.0-ios18.0, net8.0-maccatalyst18.0, net8.0-macos15.0, net8.0-tvos18.0, net8.0-ios17.5, net8.0-maccatalyst17.5, net8.0-macos14.5, netstandard2.1, net481, net462, net471

Class hierarchy
classDiagram
class Interaction~T1,T2~
class IInteraction~TInput, TOutput~ {
    <>
}
IInteraction~TInput, TOutput~ <|.. Interaction~T1,T2~

Implements: IInteraction

Remarks

Interactions allow collaborating components in an application to ask each other questions. Typically, interactions allow a view model to get the user's confirmation from the view before proceeding with some operation. The view provides the interaction's confirmation interface in a handler registered for the interaction.

Interactions have both an input and an output. Interaction inputs and outputs use generic type parameters. The interaction's input provides handlers the information they require to ask a question. The handler then provides the interaction with an output as the answer to the question.

Handlers receive an IInteractionContext, which exposes the request via Input and lets the handler respond by calling SetOutput.

By default, handlers are invoked in reverse order of registration. That is, handlers registered later are given the opportunity to handle interactions before handlers that were registered earlier. This chaining mechanism enables handlers to be registered temporarily in a specific context, such that interactions can be handled differently according to the situation. This behavior can be modified by overriding the Handle method in a subclass.

Note that handlers are not required to handle an interaction. They can choose to ignore it, leaving it for some other handler to handle. The interaction's Handle method will throw an UnhandledInteractionException if no handler handles the interaction.

Examples

<![CDATA[
 public class DeleteCustomerViewModel : ReactiveObject
 {
 public Interaction<string, bool> ConfirmDelete { get; } = new();
 
 public async Task<bool> TryDeleteAsync(string customerName)
 {
 var approved = await ConfirmDelete.Handle($"Delete {customerName}?");
 return approved;
 }
 }
 
 public partial class DeleteCustomerView : ReactiveUserControl<DeleteCustomerViewModel>
 {
 public DeleteCustomerView()
 {
 this.WhenActivated(disposables =>
 ViewModel!.ConfirmDelete.RegisterHandler(async context =>
 {
 var approved = await dialogService.ShowAsync(context.Input);
 context.SetOutput(approved);
 }).DisposeWith(disposables));
 }
 }
 ]]>

Constructors

NameSummary
.ctorRepresents an interaction between collaborating application components.

Methods

NameSummary
RegisterHandlerRegisters a synchronous interaction handler.
HandleHandles an interaction and asynchronously returns the result.
GetHandlersGets all registered handlers by order of registration.
GenerateContextGets an interaction context which is used to provide information about the interaction.
Inherited members